<?php

namespace App\Models\Application;

use Eloquent as Model;
use Illuminate\Database\Eloquent\SoftDeletes;

/**
 * Class Muticolumn
 * @package App\Models\Application
 * @version July 13, 2018, 6:44 pm CST
 *
 * @property \App\Models\Application\Sp sp
 * @property \App\Models\Application\ColumnType columnType
 * @property \App\Models\Application\Paneltpl paneltpl
 * @property \App\Models\Application\TermType termType
 * @property integer sp_id
 * @property integer column_type_id
 * @property integer level
 * @property string name
 * @property string title
 * @property string subtitle
 * @property integer paneltpl_id
 * @property integer term_type_id
 * @property string description
 */
class Muticolumn extends Model
{
    use SoftDeletes;

    public $table = 'muticolumns';

    protected $dates = ['deleted_at'];

    public $fillable = [
        'sp_id',
        'column_type_id',
        'level',
        'name',
        'title',
        'subtitle',
        // 'paneltpl_id',
        'term_type_id',
        'description',
        'listorder',
        'img',
        'short_desc',
        'data',
        // 模板选择范围
        'range',
        // 模板ID
        'tpl_id',
        // 背景图
        'bg',
        // onclick信息描述
        'message',
        // 是否可用 0不可用 1可用
        'active',
        // 栏目列表ID
        'collist_id',
    ];

    /**
     * The attributes that should be casted to native types.
     *
     * @var array
     */
    protected $casts = [
        'sp_id'          => 'integer',
        'column_type_id' => 'integer',
        'level'          => 'integer',
        'name'           => 'string',
        'collist_id'     => 'integer',
        'title'          => 'string',
        'subtitle'       => 'string',
        // 'paneltpl_id'    => 'integer',
        'term_type_id'   => 'integer',
        'description'    => 'string',
    ];

    /**
     * Validation rules
     *
     * @var array
     */
    public static $rules = [
        // 'level' => 'required',
        'name' => 'required|max:100',
    ];

    /**
     * @return \Illuminate\Database\Eloquent\Relations\HasOne
     **/
    public function sp()
    {
        return $this->hasOne(\App\Models\Common\EntityData\Channel\Sp::class, 'id', 'sp_id');
    }

    /**
     * @return \Illuminate\Database\Eloquent\Relations\HasOne
     **/
    public function columnType()
    {
        return $this->hasOne(\App\Models\Entity\Column\ColumnType::class, 'id', 'column_type_id');
    }
    /**
     *
     * 栏目列表
     *  @return \Illuminate\Database\Eloquent\Relations\HasOne
     **/
    public function collist()
    {
        return $this->hasOne(\App\Models\Entity\Column\Collist::class, 'id', 'collist_id');
    }


    /**
     * @return \Illuminate\Database\Eloquent\Relations\HasOne
     **/
    public function paneltpl()
    {
        return $this->hasOne(\App\Models\Application\Paneltpl::class, 'id', 'paneltpl_id');
    }

    /**
     * @return \Illuminate\Database\Eloquent\Relations\HasOne
     **/
    public function termType()
    {
        return $this->hasOne(\App\Models\Application\TermType::class, 'id', 'term_type_id');
    }

    /**
     * 获得此媒资的所有标签。
     */
    public function tags()
    {
        return $this->morphToMany(\App\Models\Entity\Tag\tag::class, 'tag_content');
    }

    public function tableList($where, $start, $length, $ord)
    {
        $recordsTotal    = $this->where($where)->count();
        $recordsFiltered = $recordsTotal;
        $data            = $this->with(['sp', 'columnType', 'termType', 'paneltpl'])->where($where)
            ->select()
            ->orderByRaw($ord)
            ->skip($start)->take($length)
            ->get();
        return array($recordsTotal, $recordsFiltered, $data);
    }
}
